﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Baocao
{
	public partial class Register : System.Web.UI.Page
	{
		private SqlConnection appProxy;
		private DataTable dt;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session["appProxy"] != null)
			{
				appProxy = (SqlConnection)Session["appProxy"];
				if (!IsPostBack)
				{
					if (Session["UserInfo"] != null)
					{
						dt = Session["UserInfo"] as DataTable;
						((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Company")).Text = dt.Rows[0]["Company"].ToString();
						((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Address")).Text = dt.Rows[0]["Address"].ToString();
						((Label)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("lbltkCha")).Text = dt.Rows[0]["UserID"].ToString();
					}
					else
					{
						((Label)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("lbltkCha")).Visible = false;
						((Label)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("lbtkCha")).Visible = false;
					}
				}
			}
			else
			{
				Response.Redirect("~\\Login.aspx");
			}
		}

		protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
		{
			string userid = CreateUserWizard1.UserName.Trim();
			string pass = CreateUserWizard1.Password.Trim();
			string email = CreateUserWizard1.Email.Trim();
			string fullname = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FullName")).Text.Trim();
			string company = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Company")).Text.Trim();
			string phone = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Phone")).Text.Trim();
			string address = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Address")).Text.Trim();

			try
			{
				if (Session["UserInfo"] != null)
				{
					if (((DataTable)Session["UserInfo"]).Rows[0]["ParentUser"] != null || ((DataTable)Session["UserInfo"]).Rows[0]["ParentUser"].ToString() != "")
					{
						string parentuser = ((DataTable)Session["UserInfo"]).Rows[0]["UserID"].ToString();
						string db = ((DataTable)Session["UserInfo"]).Rows[0]["FK_DatabaseID"].ToString();
						hg3.hg3.sql.SQLExecute(ref appProxy, "INSERT INTO [HiVi2SProxy].[dbo].[Userinfo]([UserID],[Password],[ParentUser],[UserName],[Company],[Phone],[Email],[Address],[FK_DatabaseID],[Status],[InDate],[OutDate]) VALUES ('" + userid + "','" + pass + "','" + parentuser + "',N'" + fullname + "',N'" + company + "','" + phone + "','" + email + "',N'" + address + "','" + db + "','0',GETDATE(),GETDATE())", 30);
					}
				}
				else
				{
					hg3.hg3.sql.SQLExecute(ref appProxy, "INSERT INTO [HiVi2SProxy].[dbo].[Userinfo]([UserID],[Password],[ParentUser],[UserName],[Company],[Phone],[Email],[Address],[FK_DatabaseID],[Status],[InDate],[OutDate]) VALUES ('" + userid + "','" + pass + "',NULL,N'" + fullname + "',N'" + company + "','" + phone + "','" + email + "',N'" + address + "',NULL,'0',GETDATE(),GETDATE())", 30);
				}
			}
			catch (Exception ex)
			{

			}
			Response.Redirect("~\\Invoice.aspx");
			return;
		}

		protected void CreateUserWizard1_ContinueButtonClick(object sender, EventArgs e)
		{

		}
	}
}
